package com.shoplink.view;

import com.shoplink.launcher.R;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.animation.LinearInterpolator;
import android.widget.RelativeLayout;

public class FocusLayout extends RelativeLayout implements
		OnFocusChangeListener {

	private FocusView changeFocus;
	private ObjectAnimator animatorX, animatorY, animatorH, animatorW,
			bgAnimatorX, bgAnimatorY, bgAnimatorH, bgAnimatorW;
	private AnimatorSet set;
	private int left, top, width, hight;
	private int leftSpace, rightSpace, topSpace, bottomSpace;
	private int bgLeftSpace, bgRightSpace, bgTopSpace, bgBottomSpace;
	private FocusView bgFocus;

	public FocusLayout(Context context) {
		super(context);

	}

	public FocusLayout(Context context, AttributeSet attrs) {
		super(context, attrs);

	}

	/**
	 * topSpace = 74; bottomSpace = 154;
	 * 
	 * rightSpace = 182; leftSpace = 91;
	 * 
	 * 
	 * bgBottomSpace = 152; bgLeftSpace = 91; bgRightSpace = 182; bgTopSpace =
	 * 76;
	 */

	void init(boolean isL) {
		if (isL) {
			topSpace = 87;
			bottomSpace = 174;

			rightSpace = 204;
			leftSpace = 102;

			// bgBottomSpace = 174;
			// bgLeftSpace = 102;
			// bgRightSpace = 204;
			// bgTopSpace = 87;
			bgBottomSpace = 174;
			bgLeftSpace = 102;
			bgRightSpace = 204;
			bgTopSpace = 87;
		} else {
			topSpace = 76;
			bottomSpace = 152;
			rightSpace = 182;
			leftSpace = 91;

			bgBottomSpace = 152;
			bgLeftSpace = 91;
			bgRightSpace = 182;
			bgTopSpace = 76;
		}

	}

	@Override
	protected void onFinishInflate() {

		super.onFinishInflate();
	}

	public void addFocusView(boolean isLauncher) {
		init(isLauncher);
		Log.d("fview", "this view size " + this.getChildCount());
		changeFocus = new FocusView(getContext());
		if (isLauncher) {
			changeFocus.setBackgroundResource(R.drawable.focus_02);
		} else {
			changeFocus.setBackgroundResource(R.drawable.focus_03);
		}

		bgFocus = new FocusView(getContext());
		bgFocus.setBackgroundResource(R.drawable.focus_01);
		Log.d("focus", this.getChildCount() + "");
		this.addView(bgFocus);
		this.addView(changeFocus);
		changeFocus.setVisibility(View.GONE);
		bgFocus.setVisibility(View.GONE);

		animatorX = ObjectAnimator.ofFloat(changeFocus, "x", 0);
		animatorY = ObjectAnimator.ofFloat(changeFocus, "y", 0);
		animatorH = ObjectAnimator.ofInt(changeFocus, "h", 0);
		animatorW = ObjectAnimator.ofInt(changeFocus, "w", 0);

		bgAnimatorX = ObjectAnimator.ofFloat(bgFocus, "x", 0);
		bgAnimatorY = ObjectAnimator.ofFloat(bgFocus, "y", 0);
		bgAnimatorH = ObjectAnimator.ofInt(bgFocus, "h", 0);
		bgAnimatorW = ObjectAnimator.ofInt(bgFocus, "w", 0);

		set = new AnimatorSet();
		set.setDuration(0);
		set.setInterpolator(new LinearInterpolator());
		set.addListener(new AnimatorListener() {

			@Override
			public void onAnimationStart(Animator arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationRepeat(Animator arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animator arg0) {
				changeFocus.setVisibility(View.VISIBLE);
				handler.sendEmptyMessageDelayed(0, 100);
			}

			@Override
			public void onAnimationCancel(Animator arg0) {
				// TODO Auto-generated method stub

			}
		});
		// set.play(animatorX).with(animatorY).with(animatorH).with(animatorW)
		// .after(bgAnimatorX).after(bgAnimatorY).after(bgAnimatorH)
		// .after(bgAnimatorW).after(300);
		// set.start();

		set.playTogether(animatorX, animatorY, animatorH, animatorW,
				bgAnimatorX, bgAnimatorY, bgAnimatorH, bgAnimatorW);
	}

	View v;

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			bgFocus.setVisibility(View.VISIBLE);
			// bgFocus.
			ObjectAnimator.ofFloat(bgFocus, "alpha", 0f, 1f).setDuration(300)
					.start();
		};
	};

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		if (hasFocus) {
			ObjectAnimator.ofFloat(bgFocus, "alpha", 1f, 0f).setDuration(0)
					.start();
			this.v = v;
			if (set != null) {
				set.end();
			}
			Rect rect = new Rect();
			v.getGlobalVisibleRect(rect);
			int[] p = new int[2];
			v.getLocationInWindow(p);

			hight = rect.height();
			width = rect.width();

			Log.d("foucsL", "l:" + p[0] + "t" + p[1] + "w:" + width);
			left = rect.left;
			top = rect.top;

			if(animatorX == null){
				return;
			}
			animatorX.setFloatValues(left - leftSpace);
			animatorY.setFloatValues(top - topSpace);
			animatorH.setIntValues(hight + bottomSpace);
			animatorW.setIntValues(width + rightSpace);

			bgAnimatorX.setFloatValues(left - bgLeftSpace);
			bgAnimatorY.setFloatValues(top - bgTopSpace);
			bgAnimatorH.setIntValues(hight + bgBottomSpace);
			bgAnimatorW.setIntValues(width + bgRightSpace);

			set.setDuration(300);
			set.start();
		}
	}

}
